package com.wwinsoft.oa.service.plan;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.activiti.engine.identity.UserQuery;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wwinsoft.framework.dao.security.AuthorityQueryDao;
import com.wwinsoft.framework.dao.security.GroupDao;
import com.wwinsoft.framework.dao.security.UserDao;
import com.wwinsoft.framework.dao.security.UserQueryDao;
import com.wwinsoft.framework.entity.security.Group;
import com.wwinsoft.framework.entity.security.User;
import com.wwinsoft.framework.pojo.security.UserBean;
import com.wwinsoft.modules.orm.Page;
import com.wwinsoft.modules.security.springsecurity.SpringSecurityUtils;
import com.wwinsoft.modules.utils.PropertiesUtils;
import com.wwinsoft.oa.dao.plan.WorkPlanDao;
import com.wwinsoft.oa.dao.plan.WorkplanQueryDao;
import com.wwinsoft.oa.entity.plan.Workplan;
import com.wwinsoft.oa.pojo.plan.WorkplanBean;

/**
 * @author: Tim.Chen
 * @since: 1.0
 */
@Service
@Transactional
@SuppressWarnings(value = { "unchecked" })
public class WorkplanService {

    @Autowired
    private WorkPlanDao workPlanDao;

    @Autowired
    private WorkplanQueryDao workplanQueryDao;

    @Autowired
    private AuthorityQueryDao authorityQueryDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserQueryDao userQueryDao;

    @Autowired
    private GroupDao groupDao;

    private final static String WORK_PLAN_AUTHORITY_NAME = "下属工作计划";

    @Transactional(readOnly = true)
    public Workplan getWorkPlan(Long id) {
        return workPlanDao.get(id);
    }

    public void deleteWorkPlan(Long id) {
        workPlanDao.delete(id);
    }

    public void saveWorkPlan(Workplan workPlan) {
        if(workPlan.getId()==null){
            String username = SpringSecurityUtils.getCurrentUser().getUsername();
            User user = userDao.getUserByUserIdWtihFilter(username);
            workPlan.setUser(user);
            if(user.getDeptId()!=null){
                Group group = groupDao.get(user.getDeptId());
                workPlan.setGroup(group);
            }
            workPlan.setStartTime(new Date());
        }
        workPlanDao.save(workPlan);
    }

    @Transactional(readOnly = true)
    public Page<Workplan> searchWorkPlanOwned(Page<Workplan> page, WorkplanBean filter) {
        Map filterMap = populateOwnedFilter(filter);
        return workplanQueryDao.searchWorkplanPage(page,filterMap);
    }

    @Transactional(readOnly = true)
    public List<Workplan> searchWorkPlanOwned(WorkplanBean filter) {
        Map filterMap = populateOwnedFilter(filter);
        return workplanQueryDao.searchWorkplan(filterMap);
    }

    @Transactional(readOnly = true)
    public Page<Workplan> searchWorkPlanSubordinate(Page<Workplan> page, WorkplanBean filter) {
        Map filterMap = populateSubFilter(filter);
        return workplanQueryDao.searchWorkplanPage(page,filterMap);
    }

    @Transactional(readOnly = true)
    public List<Workplan> searchWorkPlanSubordinate(WorkplanBean filter) {
        Map filterMap = populateSubFilter(filter);
        return workplanQueryDao.searchWorkplan(filterMap);
    }

    @Transactional(readOnly = true)
    public Page<UserBean>  searchUserNoPlanPage(Page<UserBean> page , Object filter) {
        Map filterMap = populateNoPlanFilter(filter);
        return userQueryDao.searchUserNoPlanPage(page, filterMap);
    }

    @Transactional(readOnly = true)
    public List<UserBean>  searchUserNoPlanList(Object filter) {
        Map filterMap = populateNoPlanFilter(filter);
        return userQueryDao.searchUserNoPlanList(filterMap);
    }

    private Map populateNoPlanFilter(Object filter) {
        Map filterMap = PropertiesUtils.toParameterMap(filter);
        String sqlLimit = authorityQueryDao.searchSQLLimitByAuthority(WORK_PLAN_AUTHORITY_NAME);
        if(StringUtils.isNotEmpty(sqlLimit)){
            sqlLimit = sqlLimit.replace("{0}","usr.dept_id");
        }
        filterMap.put("subordinate",true);
        filterMap.put("currentUserName", SpringSecurityUtils.getCurrentUserName());
        filterMap.put("sqlLimit",sqlLimit);
        return filterMap;
    }


    private Map populateOwnedFilter(WorkplanBean filter) {
        Map filterMap = PropertiesUtils.toParameterMap(filter);
        filterMap.put("owner", SpringSecurityUtils.getCurrentUserName());
        return filterMap;
    }

    private Map populateSubFilter(WorkplanBean filter) {
        String sqlLimit = authorityQueryDao.searchSQLLimitByAuthority(WORK_PLAN_AUTHORITY_NAME);
        if(StringUtils.isNotEmpty(sqlLimit)){
            sqlLimit = sqlLimit.replace("{0}", "workplan.group_id");
        }
        Map filterMap = PropertiesUtils.toParameterMap(filter);
        filterMap.put("currentUserName", SpringSecurityUtils.getCurrentUserName());
        filterMap.put("subordinate",true);
        filterMap.put("sqlLimit",sqlLimit);
        return filterMap;
    }

}
